var partition = function(s) {
  const ans = [], path = [];
  if (s.length === 0) return ans;

  backtracking(0, s, ans, path);
  return ans;
};

function backtracking(startIndex, s, ans, path) {
  if (startIndex >= s.length) {
    ans.push([...path]);
    return;
  }

  for (let i = startIndex; i < s.length; i++) {
    let subStr = s.substring(startIndex, i + 1);
    if (isHuiwen(subStr)) {
      path.push(subStr);
      backtracking(i + 1, s, ans, path);
      path.pop();
    } else {
      continue;
    }
  }
}

function isHuiwen(str) {
  if (str.length === 0) return false;
  let i = 0, j = str.length - 1;
  while (i <= j && str[i] === str[j]) {
    i++, j--;
  } 
  if (i > j) return true;
  return false;
}

const s = "aabaa";
console.log(partition(s))